<?php if (isset($pur_app)) { ?>
<div id="order-purapp-detail">
	<?php //var_dump($pur_app) ?>
	<div id="purapp-title">关联申请单</div>
	<div id="purapp-head">
		<input type="hidden" name="pur_app_id" value="<?php echo $pur_app['pur_app_id'] ?>" />
		<div class="number"><?php echo $pur_app['pur_app_number'] ?></div>
		<div class="person"><?php echo $pur_app['pur_app_person'] ?></div>
		<div class="date"><?php echo date('Y/m/d', $pur_app['create_date']) ?></div>
	</div>
	<!--
	<div id="purapp-content">
		<?php foreach ($pur_app['pur_app_goods'] as $pag) { ?>
		<div class="purapp-goods">
			<div class="gnumber"><?php echo $pag->sub_goods_id ?></div>
			<div class="gattr"></div>
			<div class="quantity"><?php echo $pag->quantity ?></div>
		</div>
		<?php } ?>
	</div>
	-->
</div>
<script type="text/javascript">
	//显示采购单详情
	$('#purapp-head').click(function(){
		$obj = $(this);
		purchase.detail($(this).find('input').val(), function(html){
			//alert(html);
			$o = $(html).find('.ajax-number');
			data = '{"sub_goods_ids":[';
			$o.each(function(){
				data += '"' + $(this).text() + '",';
			});
			data = data.slice(0,-1) + '],"no_action":true}';
			data = tool.toJson(data);
			url = site.url + 'goods/ajax_get_some_sub_goods/';
			$('#purapp-list-detail').remove();
			$('#purapp-head').removeAttr('style');
			style.loading('start');
			$.post(url, data, function(result){
				json = tool.toJson(tool.toJson(result).d);
				arr = [];
				$.each(json, function(k, v){
					arr[k] = v;
				});
				$obj.after($(html));
				$('#purapp-action').remove();
				//console.log(arr);
				//Start to replace the value where we get by using AJAX
				$('.ajax-number').each(function(){
					key = $(this).text();
					$(this).text(arr[key].number);
					$(this).siblings('.ajax-pro').text(arr[key].attribute).siblings('.ajax-scode').text(arr[key].search_code);
				});
				style.loading('end');
			});
		});
	});
</script>
<?php } ?>
<div id="edit-title">新建订货</div>
<div id="edit-content">
	<div id="edit-content-left">
		<form action="javascript:void(0)" id="order_create" method="POST">
			<div class="edit-name">订货编号</div>
			<div class="edit-text"><input type="text" disabled value="自动生成" /></div>
			<div class="edit-name">申请人</div>
			<div class="edit-text"><input type="text" disabled value="<?php echo $cur_user['realname'] ?>" /></div>
			<div class="edit-name">订货货品</div>
			<div class="edit-group">
				<!--ajax html-->
			</div>
			<div class="clear"></div>
			<div class="edit-submit"><input type="submit" value="新建" /></div>
		</form>
	</div>
	<div id="edit-content-right">
		<div class="edit-name">搜索订货货品<span>请键入快搜码</span><span id="search-error"></span></div>
		<div class="edit-text edit-text-search"><input type="text" /></div>
		<div id="search-result">
			<!--ajax html-->
		</div>
	</div>
	<div class="clear"></div>
</div>
<script type="text/javascript">
$('.edit-text-search input').bind('keyup', function(){
	order.goods($(this), 'main', $(this).val(), function(){
		$('#search-sub-list').children('.each-sub').click(function(){
			$each_sub = $(this);
			sub_goods_id = $(this).children('input').val();
			order.get_supplier(sub_goods_id, function(html){
				$('#order-sub-goods-supplier').remove();
				$each_sub.after($(html));
				$('#order-sub-goods-supplier .each-supplier').mouseenter(function(){$(this).css('background', '#eee')})
				.mouseleave(function(){$(this).removeAttr('style')});
				$('.each-supplier').click(function(){
					$sub_goods = $('#order-sub-goods-supplier').prev('.each-sub');
					$supplier = $(this);
					sub_goods = {
						"id"		: $sub_goods.children('input').val(),
						"number"	: $sub_goods.children('.sub-number').text(),
						"attribute"	: $sub_goods.children('.sub-attribute').text()
					}
					supplier = {
						"id"	: $supplier.children('input').val(),
						"name"	: $supplier.children('.sname').text() 
					}
					//判断是否是重复值
					$added = $('.edit-add-each');
					for (i=0; i<$added.length; i++) {
						if ($added.eq(i).children('input').eq(0).val() == sub_goods.id && $added.eq(i).children('input').eq(1).val() == supplier.id) {
							$added.eq(i).css('background', 'red');
							$added.eq(i).animate({'background':'#eee'}, 500, function(){$(this).removeAttr('style')});
							return false;
						}
					}
					//console.log(sub_goods);
					html = '';
					html += '<div class="edit-add-each">';
					html += '<input type="hidden" name="sub_goods_id[]" value="' + sub_goods.id + '" />';
					html += '<input type="hidden" name="supplier_id[]" value="' + supplier.id + '" />';
					html += '<div class="number">' + sub_goods.number + '</div>';
					html += '<div class="attribute">' + sub_goods.attribute + '</div>';
					html += '<div class="quantity"><input type="text" name="quantity[]" value="1" /></div>';
					html += '<div class="supplier-name">' + supplier.name + '</div>';
					html += '<div class="single-price"><input type="text" name="price[]" value="" /></div>';
					html += '<div class="clear"></div>';
					html += '</div>';
					$('.edit-group').append($(html));
					$('.edit-add-each .number').click(function(){
						$(this).parent('.edit-add-each').remove();
					})
				})
			});
		});
	});
});
$('form#order_create').submit(function(){
	$obj = $('.edit-add-each');
	if ($obj.length > 0) {
		$sub_goods_ids = $('input[name="sub_goods_id[]"]');
		$supplier_ids = $('input[name="supplier_id[]"]');
		$single_prices = $('input[name="price[]"]');
		$quantities = $('input[name="quantity[]"]');
		len = $sub_goods_ids.length;
		if ($supplier_ids.length == len && $single_prices.length == len && $quantities.length == len) {
			for (i=0; i<len; i++) {
				if ($single_prices.eq(i).val() <= 0 || $quantities.eq(i).val() <= 0) {
					alert('数量或单价必须大于0');
					return false;
				}
			}
			data = '{';
			temp = '';
			$sub_goods_ids.each(function(){
				temp += $(this).val() + ',';
			});
			data += '"sub_goods_ids":[' + temp.slice(0,-1) + '],';
			temp = '';
			$supplier_ids.each(function(){
				temp += $(this).val() + ',';
			});
			data += '"supplier_ids":[' + temp.slice(0,-1) + '],';
			temp = '';
			$single_prices.each(function(){
				temp += $(this).val() + ',';
			});
			data += '"single_prices":[' + temp.slice(0,-1) + '],';
			temp = '';
			$quantities.each(function(){
				temp += $(this).val() + ',';
			});
			data += '"quantities":[' + temp.slice(0,-1) + '],';
			if ($('input[name="pur_app_id"]').length == 1)
			{
				data += '"pur_app_id":' + $('input[name="pur_app_id"]').val() + ',';
			}
			data += '"create":true}';
			//alert(data);
			data = tool.toJson(data);
			url = site.url + 'purchase/ajax_order_create/';
			$.post(url, data, function(result){
				try {
					j = tool.toJson(result);
					if ('CREATE_DONE' == j.r) {
						html = '<a class="create-done" href="' + site.url + 'purchase/order/' + j.d.order_id + '" target="_self">新建成功，点击查看</a>';
						$('.edit-submit').append($(html));
					}
				}
				catch(msg) {
					alert('UNKOWN ERROR!');
				}
			});
		}
		else {
			alert('UNKOWN ERROR!');
		}
	}
	else {
		alert('请先添加子货品');
	}
})

</script>